package com.wafer.util;

import com.google.common.base.CaseFormat;

/**
 * genCode
 * <p>
 * System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test-data"));//testData
 * System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));//testData
 * System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data"));//TestData
 * <p>
 * System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "testdata"));//testdata
 * System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "TestData"));//test_data
 * System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "testData"));//test-data
 *
 * @author tangbing
 * @date 2022/4/12
 */
public class FieldUtils {

    /**
     * 下划线转驼峰 user_info => userInfo
     *
     * @param column
     * @return
     */
    public static String camel(String column) {
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, column);
    }

    /**
     * 下划线转驼峰 user_info => UserInfo
     *
     * @param column
     * @return
     */
    public static String upCamel(String column) {
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, column);
    }

    /**
     * 首字母小写 UserInfo => userInfo
     *
     * @param className 类名
     * @return 结果
     */
    public static String lowerFirstCase(String className) {
        char[] chars = className.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }

    /**
     * SQL数据类型到Java数据类型的转换
     *
     * @param sqlType SQL数据类型
     * @return Java数据类型
     */
    public static String type(String sqlType) {
        String newType;
        if (sqlType.equalsIgnoreCase("varchar")) {
            newType = "String";
        } else if (sqlType.equalsIgnoreCase("char")) {
            newType = "String";
        } else if (sqlType.equalsIgnoreCase("blob")) {
            newType = "byte[]";
        } else if (sqlType.equalsIgnoreCase("text")) {
            newType = "String";
        } else if (sqlType.equalsIgnoreCase("longtext")) {
            newType = "String";
        } else if (sqlType.equalsIgnoreCase("integer")) {
            newType = "Integer";
        } else if (sqlType.equalsIgnoreCase("tinyint")) {
            newType = "Byte";
        } else if (sqlType.equalsIgnoreCase("smallint")) {
            newType = "Integer";
        } else if (sqlType.equalsIgnoreCase("mediumint")) {
            newType = "Integer";
        } else if (sqlType.equalsIgnoreCase("bigint")) {
            newType = "Long";
        } else if (sqlType.equalsIgnoreCase("float")) {
            newType = "Float";
        } else if (sqlType.equalsIgnoreCase("double")) {
            newType = "Double";
        } else if (sqlType.equalsIgnoreCase("decimal")) {
            newType = "BigDecimal";
        } else if (sqlType.equalsIgnoreCase("date")) {
            newType = "Date";
        } else if (sqlType.equalsIgnoreCase("time")) {
            newType = "Date";
        } else if (sqlType.equalsIgnoreCase("datetime")) {
            newType = "Date";
        } else if (sqlType.equalsIgnoreCase("timestamp")) {
            newType = "LocalDateTime";
        } else if (sqlType.equalsIgnoreCase("year")) {
            newType = "Date";
        } else if (sqlType.equalsIgnoreCase("int")) {
            newType = "Integer";
        } else if (sqlType.equalsIgnoreCase("int unsigned")) {
            newType = "Integer";
        } else if (sqlType.equalsIgnoreCase("bigint unsigned")) {
            newType = "Long";
        } else if (sqlType.equalsIgnoreCase("tinyint")) {
            newType = "Byte";
        } else if (sqlType.equalsIgnoreCase("tinyint unsigned")) {
            newType = "Byte";
        } else {
            newType = sqlType;
        }
        return newType;
    }

    /**
     * SQL数据类型到Java数据类型的转换
     *
     * @param sqlType SQL数据类型
     * @return Java数据类型
     */
    public static String jdbcType(String sqlType) {
        String jdbcType;
        if (sqlType.equalsIgnoreCase("varchar")) {
            // 固定长度，最多65535个字符
            jdbcType = "VARCHAR";
        } else if (sqlType.equalsIgnoreCase("char")) {
            // 固定长度，最多255个字符
            jdbcType = "CHAR";
        } else if (sqlType.equalsIgnoreCase("blob")) {
            // 二进制
            jdbcType = "BLOB";
        } else if (sqlType.equalsIgnoreCase("longtext")) {
            // 可变长度，最多2的32次方-1个字符
            jdbcType = "LONGVARCHAR";
        } else if (sqlType.equalsIgnoreCase("text")) {
            // 可变长度，最多65535个字符
            jdbcType = "LONGVARCHAR";
        } else if (sqlType.equalsIgnoreCase("integer")) {
            jdbcType = "INTEGER";
        } else if (sqlType.equalsIgnoreCase("smallint")) {
            // 从 -2^15 (-32,768) 到 2^15 – 1 (32,767) 的整型数据。存储大小为 2个字节
            jdbcType = "SMALLINT";
        } else if (sqlType.equalsIgnoreCase("mediumint")) {
            // 3个字节,范围(-8388608~8388607)
            jdbcType = "MEDIUMINT";
        } else if (sqlType.equalsIgnoreCase("bigint")) {
            // 整型数据(所有数字)。存储大小为8个字节
            jdbcType = "BIGINT";
        } else if (sqlType.equalsIgnoreCase("float")) {
            // float(m,d) 单精度浮点型,8位精度(4字节) m总个数，d小数位
            jdbcType = "FLOAT";
        } else if (sqlType.equalsIgnoreCase("double")) {
            // double(m,d)双精度浮点型,16位精度(8字节) m总个数，d小数位
            jdbcType = "DOUBLE";
        } else if (sqlType.equalsIgnoreCase("decimal")) {
            // decimal(m,d) 参数m<65 是总个数，d<30
            jdbcType = "DECIMAL";
        } else if (sqlType.equalsIgnoreCase("date")) {
            // 日期 '2008-12-2'
            jdbcType = "DATE";
        } else if (sqlType.equalsIgnoreCase("time")) {
            // 时间 '12:25:36'
            jdbcType = "TIME";
        } else if (sqlType.equalsIgnoreCase("datetime")) {
            // 日期时间 '2008-12-2 22:06:44'
            jdbcType = "TIMESTAMP";
        } else if (sqlType.equalsIgnoreCase("timestmp")) {
            // 自动存储记录修改时间
            jdbcType = "TIMESTAMP";
        } else if (sqlType.equalsIgnoreCase("year")) {
            jdbcType = "YEAR";
        } else if (sqlType.equalsIgnoreCase("int")) {
            // 整型数据(所有数字)。存储大小为4个字节
            jdbcType = "INTEGER";
        } else if (sqlType.equalsIgnoreCase("int unsigned")) {
            // 整型数字无符号
            jdbcType = "INTEGER";
        } else if (sqlType.equalsIgnoreCase("bigint unsigned")) {
            // 长整型数字无符号
            jdbcType = "BIGINT";
        } else if (sqlType.equalsIgnoreCase("tinyint")) {
            // 从 0 到 255 的整型数据。存储大小为 1 字节
            jdbcType = "TINYINT";
        } else if (sqlType.equalsIgnoreCase("tinyint unsigned")) {
            // 从 0 到 255 的整型数据。存储大小为 1 字节 无符号
            jdbcType = "TINYINT";
        } else {
            jdbcType = sqlType;
        }
        return jdbcType;
    }

    public static String jdbcNullable(String columnNullable) {
        return "0".equals(columnNullable) ? "false" : "true";
    }
}
